package com.dwx.guli.service.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.dwx.guli.service.base.exception.GuliException;
import com.dwx.guli.service.base.result.ResultCodeEnum;
import com.dwx.guli.service.oss.service.FileService;
import com.dwx.guli.service.oss.util.OssProperties;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private OssProperties ossProperties;
    @Override
    public String upload(InputStream inputStream, String module, String oFilename) {
        String bucketName = ossProperties.getBucketName();
        String endpoint = ossProperties.getEndpoint();
        String keySecret = ossProperties.getKeySecret();
        String keyId = ossProperties.getKeyId();
        OSS ossClient=new OSSClientBuilder().build(endpoint,keyId,keySecret);
        if(!ossClient.doesBucketExist(bucketName)){
            ossClient.createBucket(bucketName);
            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        }
        String folder=new DateTime().toString("/yyyy/MM/dd/");
        String fileName= UUID.randomUUID().toString()+oFilename.substring(oFilename.lastIndexOf("."));
        String key=module+folder+fileName;
        ossClient.putObject(bucketName,key,inputStream);
        ossClient.shutdown();
        return "https://" + bucketName + "." + endpoint + "/" + key;
    }

    @Override
    public void removeFile(String url) {
        String bucketName = ossProperties.getBucketName();
        String endpoint = ossProperties.getEndpoint();
        String keySecret = ossProperties.getKeySecret();
        String keyId = ossProperties.getKeyId();
        OSS ossClient=new OSSClientBuilder().build(endpoint,keyId,keySecret);
        String host="https://" + bucketName + "." + endpoint + "/";
        String key=url.substring(host.length());
        ossClient.deleteObject(bucketName,key);
        ossClient.shutdown();
    }

    @Override
    public String upload(String url, String module) {
        String bucketName = ossProperties.getBucketName();
        String endpoint = ossProperties.getEndpoint();
        String keySecret = ossProperties.getKeySecret();
        String keyId = ossProperties.getKeyId();
        OSS ossClient=new OSSClientBuilder().build(endpoint,keyId,keySecret);
        if(!ossClient.doesBucketExist(bucketName)){
            ossClient.createBucket(bucketName);
            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        }
        String folder=new DateTime().toString("/yyyy/MM/dd/");

        String fileName= UUID.randomUUID().toString()+url.substring(url.lastIndexOf("."));
        String key=module+folder+fileName;
        InputStream inputStream=null;
        try {
            inputStream = new URL(url).openStream();
        } catch (IOException e) {
            throw new GuliException(ResultCodeEnum.CODE_ERROR);
        }
        ossClient.putObject(bucketName,key,inputStream);
        ossClient.shutdown();
        return "https://" + bucketName + "." + endpoint + "/" + key;
    }
}
